﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using NLog;

namespace Tzw.Common
{
    public static class SoapHelper
    {
        static SoapHelper()
        {
            WebRequest.DefaultWebProxy = null;
        }

        [Obsolete("改用HttpHelper")]
        public static string GetSoapReSource(string url, string dataStr, string contentType = "application/soap+xml;charset=UTF-8;action=\"HIPMessageServer\"")
        {
            Uri uri = new Uri(url);
            WebRequest webRequest = WebRequest.Create(uri);
            webRequest.ContentType = contentType;
            webRequest.Method = "POST";
            using (Stream requestStream = webRequest.GetRequestStream())
            {
                byte[] paramBytes = Encoding.UTF8.GetBytes(dataStr);
                requestStream.Write(paramBytes, 0, paramBytes.Length);
            }

            WebResponse webResponse = webRequest.GetResponse();
            using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
            {
                return TransferredMeaning.UnTransferred(myStreamReader.ReadToEnd());
            }
        }
    }

    /// <summary>
    /// 对转义字符进行处理
    /// 左尖括号: < &lt;
    /// 右尖括号: > &gt;
    /// 单引号  : ' &apos;
    /// 双引号  : " &quot;
    /// (shift+7):& &amp;
    /// </summary>
    public static class TransferredMeaning
    {
        public static string Transferred(string Meaning)
        {
            //普通字符变换成转义字符
            Meaning = Meaning.Replace("&", "&amp;");
            Meaning = Meaning.Replace("<", "&lt;");
            Meaning = Meaning.Replace(">", "&gt;");
            Meaning = Meaning.Replace("'", "&apos;");
            Meaning = Meaning.Replace("\"", "&quot;");
            return Meaning;
        }

        public static string UnTransferred(string Meaning)
        {
            //转义字符变换成普通字符
            Meaning = Meaning.Replace("&lt;", "<");
            Meaning = Meaning.Replace("&gt;", ">");
            Meaning = Meaning.Replace("&apos;", "'");
            Meaning = Meaning.Replace("&quot;", "\"");
            Meaning = Meaning.Replace("&amp;", "&");
            return Meaning;
        }
    }
}